/*
 * Created by JFormDesigner on Sun Mar 27 17:33:48 CST 2022
 */

package com.gxu.sjjg;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

/**
 * @author unknown
 */
public class FileCreate extends JFrame {
    public FileCreate() {
        initComponents();
    }
    public void create(int index) {//使用index变量区分创建哪个文件，这样可实现使用一个函数完成多个文件的操纵,即实现函数复用。
        File fg = null;
        if(index==1)//参数为1，按语文成绩生成文件
            fg=new File("src/file/Grade_orderby_chinese");
        else if(index==2)
            fg=new File("src/file/Grade_orderby_math");
        else if(index==3)
            fg=new File("src/file/Grade_orderby_english");
        else if(index==4)
            fg=new File("src/file/Grade_orderby_sport");
        else
            fg=new File("src/file/Grade_orderby_average");
        try {
            //因为成绩始终在0-100之间，所以根据成绩排序时只需要将成绩所在行的相关信息字符串存入字符串数组，
            // 并使用‘：’分隔，最后只需按分隔符划分子字符串将相应信息存入文件即可，使用的是桶排序
            String sf[]=new String[101];
            String sfa[]=new String[401];
            File f=new File("src/file/Grade.txt");
            BufferedReader bfr=new BufferedReader(new FileReader(f));
            BufferedWriter bfw=new BufferedWriter(new FileWriter(fg));//覆盖方式写入
            boolean tag = false;
            String s;
            bfr.readLine();//跳过标题栏
            while ((s = bfr.readLine()) != null) {
                if(index==5){//平均成绩需要计算后得出；按平均分排序可以转化为对总分的排序。
                    int grade=0;
                    for(int i=2;i<=5;i++)
                        grade=grade+Integer.parseInt(s.substring(i*10, i*10+9).trim());
                    if(sfa[grade]==null)
                        sfa[grade]="";
                    sfa[grade]=sfa[grade]+s.substring(0, 19)+ grade/4.0+":";
                    continue;
                }
                int grade=Integer.parseInt(s.substring((index+1)*10, (index+1)*10+9).trim());
                if(sf[grade]==null)
                    sf[grade]="";
                sf[grade]=sf[grade]+s.substring(0, 19)+s.substring((index+1)*10, (index+1)*10+9)+":";//获取子字符串,":"用以分割
            }
            bfw.write("姓名       学号       ");
            if(index==1)
                bfw.write("语文");
            else if(index==2)
                bfw.write("数学");
            else if(index==3)
                bfw.write("英语");
            else if(index==4)
                bfw.write("体育");
            else if(index==5)
                bfw.write("平均成绩");
              bfw.newLine();
            if(index<5)
                for(int i=100;i>=0;i--){
                    if(sf[i]==null)//没有该分的数据，就跳过
                        continue;
                    String ss[]=sf[i].split(":");//把sf[i]存的字符串拆分存入一个字符串数组中
                    for(String x:ss){//将同分数的学生信息写入文件
                        bfw.write(x);
                        bfw.newLine();
                    }
                }
            else
                for(int i=400;i>=0;i--){
                    if(sfa[i]==null)//没有该分的数据，就跳过
                        continue;
                    String ss[]=sfa[i].split(":");//把ss[i]存的字符串拆分存入一个字符串数组中
                    for(String x:ss){//将同分数的学生信息写入文件
                        bfw.write(x);
                        bfw.newLine();
                    }
                }
            bfw.close();
            bfr.close();
            JOptionPane.showMessageDialog(null, "文件已生成或刷新！", "提示", JOptionPane.INFORMATION_MESSAGE);//带蓝色！图标
            //JOptionPane.showMessageDialog(null, "", "", JOptionPane.WARNING_MESSAGE);//带黄色！图标
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private void chinese(ActionEvent e) {
        create(1);
    }
    private void math(ActionEvent e) {
        create(2);
    }
    private void english(ActionEvent e) {
        create(3);
    }
    private void sport(ActionEvent e) {
        create(4);
    }
    private void average(ActionEvent e) {
        create(5);
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        panel1 = new JPanel();
        button1 = new JButton();
        button2 = new JButton();
        button3 = new JButton();
        button4 = new JButton();
        button5 = new JButton();
        bgp = new JLabel();

        //======== this ========
        setVisible(true);
        setTitle("\u751f\u6210\u6587\u4ef6");
        setResizable(false);
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== panel1 ========
        {
            panel1.setLayout(null);

            //---- button1 ----
            button1.setText("\u6309\u8bed\u6587\u6210\u7ee9\u6392\u5e8f");
            button1.addActionListener(e -> chinese(e));
            panel1.add(button1);
            button1.setBounds(100, 10, 125, button1.getPreferredSize().height);

            //---- button2 ----
            button2.setText("\u6309\u6570\u5b66\u6210\u7ee9\u6392\u5e8f");
            button2.addActionListener(e -> math(e));
            panel1.add(button2);
            button2.setBounds(100, 60, 125, 28);

            //---- button3 ----
            button3.setText("\u6309\u82f1\u8bed\u6210\u7ee9\u6392\u5e8f");
            button3.addActionListener(e -> english(e));
            panel1.add(button3);
            button3.setBounds(100, 110, 125, 28);

            //---- button4 ----
            button4.setText("\u6309\u5e73\u5747\u6210\u7ee9\u6392\u5e8f");
            button4.addActionListener(e -> average(e));
            panel1.add(button4);
            button4.setBounds(100, 210, 130, 28);

            //---- button5 ----
            button5.setText("\u6309\u4f53\u80b2\u6210\u7ee9\u6392\u5e8f");
            button5.addActionListener(e -> sport(e));
            panel1.add(button5);
            button5.setBounds(100, 160, 130, 28);

            //---- bgp ----
            bgp.setIcon(new ImageIcon(getClass().getResource("/myimage/loginimg.jpg")));
            panel1.add(bgp);
            bgp.setBounds(0, 0, 315, 280);
        }
        contentPane.add(panel1);
        panel1.setBounds(0, 0, 315, 280);

        contentPane.setPreferredSize(new Dimension(315, 310));
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel panel1;
    private JButton button1;
    private JButton button2;
    private JButton button3;
    private JButton button4;
    private JButton button5;
    private JLabel bgp;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
